var AjaxUtil = {
	// 用POST方法请求数据
	postRequest : function(url, params, fn) {
		var self = this;
		if (!url) {
			fn && fn.call(self);
			return;
		}
		$.ajax( {
			type : "POST",
			url : url,
			dataType : 'json',
			data : params,
			success : function(data) {
				AjaxUtil.processData(data);
				fn && fn.call(self, data);
			},
			error : function() {
				alert("数据请求异常！");
			}
		})
	},
	getObjByUrl : function(url,hasCloth) {
		if(url.indexOf('?')==-1) 
			url = url +"?rp=0";
		else if(url.indexOf('rp=0')==-1){
			url=url + "&rp=0";
		}
		if(!hasCloth)
			url = url + "&needCloth=false";
		var ret  ;
		$.ajax({
			type:"POST",
			url:url,
			dataType:"json",
			async:false,
			traditional:true,
			success:function(e) {
				AjaxUtil.processData(e);
				ret = e;
			},
           error:function(jqXHR, textStatus, errorThrown){
           	if(errorThrown)
           		alert(textStatus);
           		
           }
		});
		return ret;
	},
	
	
	//通过url属性请求远程数据
	request : function (url,params,fn) {
		var self = this;
		if (!url) {
			fn && fn.call(self);
			return;
		}
		$.ajax({
			url: url,
			dataType: 'json',
			data:params,
			traditional:true,
			success: function (data) {
				AjaxUtil.processData(data);
				fn && fn.call(self,data);
			},
			error: function () {
				alert("数据请求异常！");
			}
		})
	},
	/**
	 * 以item的格式返回
	 */
	getItemsByUrl : function(url,valueName,textName) {
		if(url.indexOf('?')==-1) 
			url = url +"?rp=0";
		else if(url.indexOf('rp=0')==-1){
			url=url + "&rp=0";
		}
		url = url + "&needCloth=false";
		var itemArr = [];
		$.ajax({
			type:"POST",
			url:url,
			dataType:"json",
			async:false,
			traditional:true,
			success:function(e) {
				AjaxUtil.processData(e);
				if(e.msg == "") {
					var data = e.rows;
					var tempObj;
					for(var i=0,len=data.length;i<len;i++) {
						tempObj = {};
						tempObj.text = data[i].cell[textName];
						tempObj.value = data[i].cell[valueName];
						itemArr.push(tempObj);
					}
				} 
			}
		});
		return itemArr;
	},
	/**
	 * 根据valueName返回
	 */
	getArrayByUrl : function(url,valueName) {
		if(url.indexOf('?')==-1) 
			url = url +"?rp=0";
		else if(url.indexOf('rp=0')==-1){
			url=url + "&rp=0";
		}
		url = url + "&needCloth=false";
		var itemArr = [];
		$.ajax({
			type:"POST",
			url:url,
			dataType:"json",
			async:false,
			success:function(e) {
				AjaxUtil.processData(e);
				if(e.msg == "") {
					var data = e.rows;
					
					for(var i=0,len=data.length;i<len;i++) {
						
						itemArr.push(data[i].cell[valueName]);
					}
				} 
			}
		});
		return itemArr;
	},
	/**
	 * 根据url 返回一个objct数组
	 */
	getObjArray: function(url,cloth,param){
		if(url.indexOf('?')==-1) 
			url = url +"?rp=0";
		else if(url.indexOf('rp=0')==-1){
			url=url + "&rp=0";
		}
		url = url + "&needCloth=false";
		var objArr = [];
		$.ajax({
			type:"POST",
			url:url,
			dataType:"json",
			async:false,
			data:param,
			success:function(e) {
				AjaxUtil.processData(e);
				if(e.msg == "") {
					var data = e.rows;
					
					for(var i=0,len=data.length;i<len;i++) {
						var obj = data[i].cell;
						if(cloth){
							obj = cloth(obj);
						}
						objArr.push(obj);
					}
				} 
			}
		});
		return objArr;
	},
	processData:function(_data){
		if(!_data)return;
    	
        
        if(_data.msg ){
        	alert(_data.msg);
        }
        if(_data.callBackFun){
        	var fun = eval(_data.callBackFun);
        	if(fun)
        		fun(_data);
        }
	}
}

/**
 * 超时登出方法，写在这里不是很好，暂时先这么处理
 * 如果更新次文件需要注意下
 * @return
 */
function goLogin(data) {
	if (!parent){
		if (!window.logout){
			window.logout = true;
			alert(data.msg);
		}
	} else{
		if (!parent.logout){
			parent.logout = true;
			alert(data.msg);
		}
	}
	
	var url = getPath() + "/login.htm";
	var urlTop = "" + top.location;
	var url = urlTop.substring(0,urlTop.lastIndexOf('/'));
	url +=  "/login.htm";
	//alert(url);
	top.location = url;
	
/*	if (!parent) {
		top.location = url;
	}
	if (parent.loginLocation){
		top.location = parent.loginLocation;
	}
	else {
		top.location = urlTop;
	}*/
}

function getPath(){
	var strFullPath=window.document.location.href;
	var strPath=window.document.location.pathname;
	var pos=strFullPath.indexOf(strPath);
	var prePath=strFullPath.substring(0,pos);
	var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
	return prePath+postPath;
}
